package logic

import (
	"context"
	"doctor-medicine-system/docrpc/docrpc"
	"doctor-medicine-system/docrpc/internal/svc"
	"doctor-medicine-system/models/mysql"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UpdatesLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatesLogic {
	return &UpdatesLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatesLogic) Updates(in *docrpc.UpdatesRequest) (*docrpc.UpdatesResponse, error) {
	// todo: add your logic here and delete this line
	var drug mysql.Drug
	if err := drug.FIndDrugID(mysql.DB, in.DurgId); err != nil {
		return nil, errors.New("药品id不存在")
	}
	drug = mysql.Drug{
		Model:      gorm.Model{ID: uint(in.DurgId)},
		Norms:      in.Norms,
		Vendor:     in.Vendor,
		Indication: in.Indication,
		Dosage:     in.Dosage,
		Matter:     in.Matter,
		DocTocId:   in.DocTocId,
	}

	if err := drug.Update(mysql.DB); err != nil {
		return nil, err
	}
	return &docrpc.UpdatesResponse{
		DurgId: int64(drug.ID),
	}, nil
}
